<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-select
        v-model="dataForm.vip"
        filterable
        placeholder="是否免费"
        @change="selectVip"
      >
        <el-option
          v-for="item in optionsVip"
          :key="item.status"
          :label="item.label"
          :value="item.status"
        >
        </el-option>
      </el-select>
      <el-form-item>
        <el-input
          v-model="dataForm.key"
          placeholder="用户昵称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column
        :index="indexMethod"
        label="#"
        prop="Serial_number"
        sortable
        type="index"
      ></el-table-column>
      <el-table-column
        prop="avatar"
        header-align="center"
        align="center"
        label="头像"
      >
        <template slot-scope="scope">
          <div class="col-sm-3">
            <el-image
              style="width: 100px; height: 50px"
              :src="scope.row.avatar"
              fit="contain"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="nickname"
        header-align="center"
        align="center"
        label="昵称"
      >
      </el-table-column>
      <el-table-column
        prop="mobile"
        header-align="center"
        align="center"
        :formatter="handlderMobile"
        label="手机号"
      >
      </el-table-column>
      <el-table-column label="性别">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.gender === 1">男</el-tag>
          <el-tag type="warning" v-else-if="scope.row.gender === 2">女</el-tag>
          <el-tag type="primary" v-else>未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否是VIP">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.vip === 1">是</el-tag>
          <el-tag type="warning" v-else-if="scope.row.vip === 0">否</el-tag>
          <el-tag type="primary" v-else>未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="vipExpiresTime"
        header-align="center"
        align="center"
        :formatter="handlderVipExpiresTime"
        label="vip过期时间"
        width="160"
      >
      </el-table-column>
      <el-table-column label="用户来源">
        <template slot-scope="scope">
          <el-tag type="" v-if="scope.row.origin === 0">APP注册</el-tag>
          <el-tag type="primary" v-if="scope.row.origin === 1">微信注册</el-tag>
          <el-tag type="success" v-else-if="scope.row.origin === 2"
            >腾讯QQ</el-tag
          >
          <el-tag type="warning" v-else-if="scope.row.origin === 3"
            >新浪微博</el-tag
          >
          <el-tag type="danger" v-else-if="scope.row.origin === 4"
            >Apple注册</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="注册时间"
        width="160"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <!--  <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>-->
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./user-add-or-update";
export default {
  data() {
    return {
      optionsVip: [
        {
          status: "",
          label: "全部状态"
        },
        {
          status: "1",
          label: "免费"
        },
        {
          status: 2,
          label: "VIP+视频"
        },
        {
          status: 3,
          label: "仅VIP"
        },
        {
          status: 4,
          label: "仅视频"
        }
      ],
      dataForm: {
        key: "",
        vip: ""
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    };
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList();
  },
  methods: {
    indexMethod(index) {
      return index + 1;
    },
    selectVip(value) {
      this.dataForm.vip = value;
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/theme/user/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          vip: this.dataForm.vip,
          key: this.dataForm.key
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    handlderMobile(row, column) {
      if (row.mobile === null || row.mobile === "") {
        return "-";
      }
      return row.mobile;
    },
    handlderVipExpiresTime(row, column) {
      if (row.vipExpiresTime === null || row.vipExpiresTime === "") {
        return "-";
      }
      return row.vipExpiresTime;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm(
        `确定对已选[id=${ids.join(",")}]的用户进行[${
          id ? "删除" : "批量删除"
        }]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/theme/user/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    }
  }
};
</script>
